﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;

namespace MyWebPagesStarterKit.Utils
{
    /// <summary>
    /// FilterRows, helper for get a number of rows from a datatable.
    /// </summary>
    public class FilterRows : IEnumerable
    {
        private DataView dataView;
        private int rowsToShow;

        public FilterRows(DataView dataView, int rowsToShow)
        {
            this.rowsToShow = rowsToShow;
            this.dataView = dataView;
        }

        public IEnumerator GetEnumerator()
        {
            return new PageOfData(this.dataView.GetEnumerator(), this.rowsToShow);
        }

        internal class PageOfData : IEnumerator
        {
            private int cnt;
            private IEnumerator e;
            private int rowsToShow;

            internal PageOfData(IEnumerator e, int rowsToShow)
            {
                this.rowsToShow = rowsToShow;
                this.e = e;
            }

            public bool MoveNext()
            {
                if (this.cnt >= this.rowsToShow)
                {
                    return false;
                }
                this.cnt++;
                return this.e.MoveNext();
            }

            public void Reset()
            {
                this.e.Reset();
                this.cnt = 0;
            }

            public object Current
            {
                get
                {
                    return this.e.Current;
                }
            }
        }
    }
}